-- summon
-- created by panyl
-- 地牢格子：召唤兽

return {
    restoreGrid = function(grid, info)
        -- 重新创建一个
        info["pos"] = grid:getPos();

        if info.bonus then
            grid.bonus = info.bonus;
            info.bonus = nil;
        end

        -- 是否漂浮闪光
        if info.need_shadow then
            grid.need_shadow = info.need_shadow;
        end
        if info.need_shining then
            grid.need_shining = info.need_shining;
        end

        -- 召唤兽未死亡
        if info.state ~= GRID_STATE_DISABLE then
            local summon = SummonM.createSummon(info);
            summon.gridPos = grid.pos;
            grid.summon = summon;

            -- 刷出的召唤兽记录在玩家 temp 数据中，以便后续查找
            local summon_list = ME.user.dbase:queryTemp("summon_list", {});
            table.insert(summon_list, summon);
            ME.user.dbase:setTemp("summon_list", summon_list);

            PropM.refresh(summon);
        end
    end,

    collectContext = function(grid)
        -- 收集
        local summon = grid.summon;
        if not summon or summon:isDead() then
            local ret = {["bonus"] = grid.bonus, };
            if grid.need_shadow then
                table.append(ret, {["need_shadow"] = grid.need_shadow});
            end
            if grid.need_shining then
                table.append(ret, {["need_shining"] = grid.need_shining});
            end
            return ret;
        end

        if SummonM.query(summon.classId, "carry_pass") == 1 then
            -- 保存下固定数据
            local pos = grid:getPos();
            local summonInfo = ME.user.dbase:query("summon", {})[pos];
            if type(summonInfo) == "table" then
                if DungeonInstanceM.isInInstance() then
                    summonInfo["instance_pos"] = pos;
                else
                    summonInfo["normal_pos"] = pos;
                end
            end

            return { ["state"] = GRID_STATE_DISABLE, ["bonus"] = grid.bonus, };
        end

        return {
            ["hp"] = summon.dbase:query("hp", 0),
            ["max_hp"] = summon.dbase:query("max_hp", 0),
            ["attack"] = summon.dbase:query("attack", 0),
            ["accuracy"] = summon.dbase:query("accuracy", 0),
            ["dodge"] = summon.dbase:query("dodge", 0),
            ["absorb_rate"] = summon.dbase:query("absorb_rate", 0),
            ["class_id"] = summon.dbase:query("class_id"),
            ["disappear"] = summon:query("disappear"),
            ["active_round"] = summon.active_round,
            ["attack_round"] = summon.dbase:query("attack_round", 0),
            ["cast_skill"]   = summon.dbase:query("cast_skill"),
            ["action"]       = summon.action,
            ["bonus"]        = grid.bonus,
            ["mp"] = summon:query("mp"),
            ["born"] = summon.born,
            ["combat_status"] = summon.dbase:query("combat_status"),
        };
    end
};
